package com.example.gc.oams.zpx.voucher.util;

import com.example.gc.oams.zpx.voucher.model.AccountingFlow;
import com.thames.util.BeanUtil;
import com.thames.util.StringUtil;
import org.apache.commons.lang3.time.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class AccountingFlowUtil {

    /**
     * 数据库字段
     */
    public static final List<String> FIELDS_SORT = new ArrayList<>();

    /**
     * 数据库字段映射对象属性
     */
    public static final Map<String, String> FIELD_TO_ATTRIBUTES = new ConcurrentHashMap<>();

    static {
        FIELDS_SORT.add("loan_order_no");
        FIELDS_SORT.add("repayment_no");
        FIELDS_SORT.add("account_voucher_no");
        FIELDS_SORT.add("voucher_type_code");
        FIELDS_SORT.add("accounting_date");
        FIELDS_SORT.add("gen_date");
        FIELDS_SORT.add("sync_date");
        FIELDS_SORT.add("sync_jde_num");
        FIELDS_SORT.add("base_enterprise_id");
        FIELDS_SORT.add("company_id");
        FIELDS_SORT.add("jde_doc_no");
        FIELDS_SORT.add("jde_icu_no");
        FIELDS_SORT.add("fail_remark");
        FIELDS_SORT.add("sync_status");
        FIELDS_SORT.add("gen_status");
        FIELDS_SORT.add("create_time");
        FIELDS_SORT.add("create_user");
        FIELDS_SORT.add("modify_time");
        FIELDS_SORT.add("modify_user");
        FIELDS_SORT.add("department");
        FIELDS_SORT.add("rate_settlement_bill_no");
        FIELDS_SORT.add("purchase_order_no");
        FIELDS_SORT.add("for_payment_no");
        FIELDS_SORT.add("return_order_no");

        FIELD_TO_ATTRIBUTES.put("account_voucher_no", "accountVoucherNo");
        FIELD_TO_ATTRIBUTES.put("sync_jde_num", "syncJdeNum");
        FIELD_TO_ATTRIBUTES.put("base_enterprise_id", "baseEnterpriseId");
        FIELD_TO_ATTRIBUTES.put("purchase_order_no", "purchaseOrderNo");
        FIELD_TO_ATTRIBUTES.put("modify_user", "modifyUser");
        FIELD_TO_ATTRIBUTES.put("modify_time", "modifyTime");
        FIELD_TO_ATTRIBUTES.put("voucher_type_code", "voucherTypeCode");
        FIELD_TO_ATTRIBUTES.put("loan_order_no", "loanOrderNo");
        FIELD_TO_ATTRIBUTES.put("jde_doc_no", "jdeDocNo");
        FIELD_TO_ATTRIBUTES.put("department", "department");
        FIELD_TO_ATTRIBUTES.put("gen_status", "genStatus");
        FIELD_TO_ATTRIBUTES.put("accounting_date", "accountingDate");
        FIELD_TO_ATTRIBUTES.put("fail_remark", "failRemark");
        FIELD_TO_ATTRIBUTES.put("sync_date", "syncDate");
        FIELD_TO_ATTRIBUTES.put("rate_settlement_bill_no", "rateSettlementBillNo");
        FIELD_TO_ATTRIBUTES.put("jde_icu_no", "jdeIcuNo");
        FIELD_TO_ATTRIBUTES.put("for_payment_no", "forPaymentNo");
        FIELD_TO_ATTRIBUTES.put("company_id", "companyId");
        FIELD_TO_ATTRIBUTES.put("create_time", "createTime");
        FIELD_TO_ATTRIBUTES.put("gen_date", "genDate");
        FIELD_TO_ATTRIBUTES.put("create_user", "createUser");
        FIELD_TO_ATTRIBUTES.put("repayment_no", "repaymentNo");
        FIELD_TO_ATTRIBUTES.put("sync_status", "syncStatus");
        FIELD_TO_ATTRIBUTES.put("return_order_no", "returnOrderNo");
    }

    /**
     * 生成SQL语句
     *
     * @param accountingFlow
     * @return
     */
    public static String genAccountingFlowSqlScript(AccountingFlow accountingFlow) {
        StringBuffer sb = new StringBuffer();
        sb.append("insert into capital_qa.accounting_flow(");
        sb.append(StringUtil.join(FIELDS_SORT)).append(") values(");

        Map<String, Object> flowMap = BeanUtil.asMap(accountingFlow);
        for (int i = 0; i < FIELDS_SORT.size(); i++) {
            if (i != 0) {
                sb.append(",");
            }

            Object value = flowMap.get(FIELD_TO_ATTRIBUTES.get(FIELDS_SORT.get(i)));
            if (value == null) {
                sb.append("null");
            } else if (value instanceof String) {
                sb.append("'").append(value.toString()).append("'");
            } else if (value instanceof Date) {
                sb.append("'").append(StringUtil.format(VoucherUtils.YYYY_MM_DD_HH_MM_SS_2, (Date) value)).append("'");
            } else {
                sb.append(value.toString());
            }
        }

        sb.append(");\n");

        return sb.toString();
    }


    private AccountingFlowUtil() {
    }

}
